package com.nantai.jiafang.nantaijiafang.nt5s.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nantai.jiafang.nantaijiafang.R;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */

public class GridViewAdapter extends BaseAdapter {
    public LayoutInflater inflater;
    public List<String> imageUrls;

    public GridViewAdapter(Context context, List<String> imageUrls) {
        inflater = LayoutInflater.from(context);
        this.imageUrls = imageUrls;
    }
    @Override
    public int getCount() {
        return imageUrls.size();
    }

    @Override
    public String getItem(int position) {
        return imageUrls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String currStr = getItem(position);
        View view;
        ViewHolder holder;
        if(convertView == null)
        {
            view = inflater.inflate(R.layout.item_grid, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) view.findViewById(R.id.img_grid);
            view.setTag(holder);
        }else
        {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        //为防止图片出现乱序，对每个ImageView进行绑定设置
        holder.imageView.setTag(currStr);
        //设置默认的显示图片
        showBitmapToImageView(currStr, holder.imageView);
        return view;
    }

    /**
     * 设置Bitmap图片到ImageView上面
     * @param url
     * @param imageView
     */
    public void showBitmapToImageView(String url,ImageView imageView)
    {
        x.image().bind(imageView, url);
    }
    static class ViewHolder
    {
        ImageView imageView;
    }
}
